Skip to content

CSHARP-4935: Support casting from an interface to a type that implements that interface in LINQ queries#1419

Merged
rstam merged 1 commit intomongodb:mainfrom
rstam:csharp4935
Apr 29, 2025
Merged

CSHARP-4935: Support casting from an interface to a type that implements that interface in LINQ queries#1419
rstam merged 1 commit intomongodb:mainfrom
rstam:csharp4935

Conversation

@rstam
Copy link
Copy Markdown
Contributor

@rstam rstam commented Aug 16, 2024

No description provided.

@rstam rstam requested a review from a team as a code owner August 16, 2024 01:12
@rstam rstam requested review from adelinowona and sanych-sun and removed request for a team and adelinowona August 16, 2024 01:12
Copy link
Copy Markdown
Contributor Author

@rstam rstam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The LINQ changes in this PR look fine to me.

The concern we had that has kept this on the back burner is whether the change to DiscriminatedInterfaceSerializer would be backward breaking in any way.

_interfaceType = typeof(TInterface);
_discriminatorConvention = discriminatorConvention ?? BsonSerializer.LookupDiscriminatorConvention(typeof(TInterface));
_objectSerializer = BsonSerializer.LookupSerializer<object>();
_objectSerializer = objectSerializer ?? new ObjectSerializer(allowedTypes: type => typeof(TInterface).IsAssignableFrom(type));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you see any backward compatibility issues with this change?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a problem if someone registered their own object serializer for some reasons. Can we implement method like a WithAllowedTypes on objectSerializer? So we lookup the same way as it was before - and then try to use this new method, similar to this:

var objectSerializer = BsonSerializer.LookupSerializer<object>();
if (objectSerializer is ObjectSerializer builtInSerializer)
{
    objectSerializer  = builtInSerializer.WithAllowedTypes(allowedTypes: type => typeof(TInterface).IsAssignableFrom(type));
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good idea. I've made the change.

_interfaceType = typeof(TInterface);
_discriminatorConvention = discriminatorConvention ?? BsonSerializer.LookupDiscriminatorConvention(typeof(TInterface));
_objectSerializer = BsonSerializer.LookupSerializer<object>();
_objectSerializer = objectSerializer ?? new ObjectSerializer(allowedTypes: type => typeof(TInterface).IsAssignableFrom(type));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a problem if someone registered their own object serializer for some reasons. Can we implement method like a WithAllowedTypes on objectSerializer? So we lookup the same way as it was before - and then try to use this new method, similar to this:

var objectSerializer = BsonSerializer.LookupSerializer<object>();
if (objectSerializer is ObjectSerializer builtInSerializer)
{
    objectSerializer  = builtInSerializer.WithAllowedTypes(allowedTypes: type => typeof(TInterface).IsAssignableFrom(type));
}

else
_interfaceSerializer = interfaceSerializer;

if (objectSerializer == null)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a user passes in an objectSerializer we should use it as-is with no further configuration on our part. It is up to the user to pass in a properly configured seiralizer.

Copy link
Copy Markdown
Member

@sanych-sun sanych-sun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rstam rstam changed the title CSHARP-4935: Linq3Implementation unable to handle interface as root CSHARP-4935: Support casting from an interface to a type that implements that interface in LINQ queries Apr 29, 2025
@rstam rstam requested a review from sanych-sun April 29, 2025 22:51
@rstam rstam added the feature Adds new user-facing functionality. label Apr 29, 2025
Copy link
Copy Markdown
Member

@sanych-sun sanych-sun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rstam rstam merged commit 97f2667 into mongodb:main Apr 29, 2025
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Adds new user-facing functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants